/*
 * This program is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software 
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this 
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html 
 * or from the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright 2008 Pentaho Corporation.  All rights reserved.
 *
 * @author Will Gorman (wgorman@pentaho.com)
 */
package org.pentaho.actioneditor.client.actionpanels;

import org.pentaho.actioneditor.client.ActionEditor;
import org.pentaho.actionsequence.dom.ActionInputConstant;
import org.pentaho.actionsequence.dom.actions.SqlQueryAction;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

public class SqlQueryActionPanel extends AbstractActionPanel {
  final SqlQueryActionUI aui = new SqlQueryActionUI();
  SqlQueryAction action;
  

  
  public SqlQueryActionPanel(SqlQueryAction action, ActionEditor actionEditor) {
    this.action = action;
    this.actionEditor = actionEditor;  
  }
  
  public Widget getRoot() {
    return aui.getRoot();
  }
  
  public void init() {
    aui.init();

    aui.getName().setText(action.getDescription());
    aui.getName().addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        action.setDescription(aui.getName().getText());
        item.setText(aui.getName().getText());
      }
    });
    
    if (action.getJndi() != null) {
      aui.getJndiRadio().setChecked(true);
      aui.getJndiName().setText(getActionInputString(action.getJndi()));
      aui.getConnectionTypeDock().showWidget(1); 
    } else if (action.getSharedConnection() != null) {
      aui.getSharedRadio().setChecked(true);
      aui.getSharedName().setText(getActionInputString(action.getSharedConnection()));
      aui.getConnectionTypeDock().showWidget(2);
    } else {
      // assume JDBC
      aui.getJdbcRadio().setChecked(true);
      aui.getJdbcDriver().setText(getActionInputString(action.getDriver()));
      aui.getDatabaseUrl().setText(getActionInputString(action.getDbUrl()));
      aui.getUsername().setText(getActionInputString(action.getUserId()));
      aui.getPassword().setText(getActionInputString(action.getPassword()));
      aui.getConnectionTypeDock().showWidget(0);
    }
    
    aui.getJndiRadio().addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        aui.getConnectionTypeDock().showWidget(1);
        action.setJndi(new ActionInputConstant("", null));
        aui.getJndiName().setText("");
      }
    });

    aui.getSharedRadio().addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        aui.getConnectionTypeDock().showWidget(2);
        //action.setJndi(new ActionInputConstant(""));
        // action.setSharedConnection(null);
        aui.getSharedName().setText("");
      }
    });

    aui.getJdbcRadio().addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        aui.getConnectionTypeDock().showWidget(0);
        action.setDriver(new ActionInputConstant("", null));
        action.setDbUrl(new ActionInputConstant("", null));
        action.setUserId(new ActionInputConstant("", null));
        action.setDriver(new ActionInputConstant("", null));
        aui.getJdbcDriver().setText("");
        aui.getDatabaseUrl().setText("");
        aui.getUsername().setText("");
        aui.getPassword().setText("");
      }
    });
    
    aui.getJndiName().addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        action.setJndi(getProviderFromString(aui.getJndiName().getText()));
      }
    });

    aui.getSharedName().addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        action.setSharedConnection(getProviderFromString(aui.getSharedName().getText()));
      }
    });

    // JDBC
    
    aui.getJdbcDriver().addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        action.setDriver(getProviderFromString(aui.getJdbcDriver().getText()));
      }
    });

    aui.getDatabaseUrl().addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        action.setDbUrl(getProviderFromString(aui.getDatabaseUrl().getText()));
      }
    });
    
    aui.getUsername().addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        action.setUserId(getProviderFromString(aui.getUsername().getText()));
      }
    });

    aui.getPassword().addChangeListener(new ChangeListener() {
      public void onChange(Widget sender) {
        action.setPassword(getProviderFromString(aui.getPassword().getText()));
      }
    });

    // live / memory
    
    if (action.getLive().getBooleanValue(false)) {
      aui.getLiveRadio().setChecked(true);
    } else {
      aui.getMemoryRadio().setChecked(true);
    }
  
    aui.getLiveRadio().addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        action.setLive(new ActionInputConstant(true, null));
      }
    });
    aui.getMemoryRadio().addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        action.setLive(new ActionInputConstant(false, null));
      }
    });
    
    // query
    
    aui.getQuery().setText(getActionInputString(action.getQuery()));
    aui.getQuery().addChangeListener( new ChangeListener() {
      public void onChange(Widget sender) {
        action.setQuery(getProviderFromString(aui.getQuery().getText()));
      }
    });
    
    // check box
    
    if (action.getOutputPreparedStatement() != null) {
      aui.getSubreportRadio().setChecked(true);
      aui.getResultSetDeck().showWidget(1);
      aui.getSubqueryName().setText(action.getOutputPreparedStatement().getPublicName());
    } else {
      aui.getImmediateRadio().setChecked(true);
      aui.getResultSetDeck().showWidget(0);
      if (action.getOutputResultSet() != null) {
        aui.getResultSetName().setText(action.getOutputResultSet().getPublicName());
      }
    }

    aui.getImmediateRadio().addClickListener(new ClickListener() {

      public void onClick(Widget sender) {
        aui.getResultSetDeck().showWidget(0);
        if (action.getOutputPreparedStatement() != null) {
          action.setOutputResultSet(action.getOutputPreparedStatement().getPublicName());
          aui.getResultSetName().setText(action.getOutputResultSet().getPublicName());
        }
      }
    });
    
    aui.getSubreportRadio().addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        aui.getResultSetDeck().showWidget(1);
        if (action.getOutputResultSet() != null) {
          action.setOutputPreparedStatement(action.getOutputResultSet().getPublicName());
          aui.getSubqueryName().setText(action.getOutputPreparedStatement().getPublicName());
        }
      }
    });
    
    // Result set name

    aui.getResultSetName().addChangeListener( new ChangeListener() {
      public void onChange(Widget sender) {
        action.setOutputResultSet(aui.getResultSetName().getText());
        item.getChild(0).setHTML(aui.getResultSetName().getText());
        actionEditor.updateOutputMenuBar();

      }
    });
    
    aui.getSubqueryName().addChangeListener( new ChangeListener() {
      public void onChange(Widget sender) {
        action.setOutputPreparedStatement(aui.getSubqueryName().getText());
        item.getChild(0).setHTML(aui.getResultSetName().getText());
        actionEditor.updateOutputMenuBar();
      }
    });
  }
  

}
